<!--
 * @Description: 分配部门/岗位
 * @FilePath: \hk-computerRoom-Security\src\views\system\user\assignDeptPost.vue
 * @Date: 2024-01-18 09:14:09
 * @LastEditTime: 2024-01-18 09:59:13
-->
<template>
	<div class="system-user-dialog-container energy-from energy-dialog energy-tree">
		<el-dialog center align-center v-model="state.dialog.isShowDialog" width="40%" @close="resetData">
			<template #header="{ titleClass }">
				<div :class="titleClass">分配部门/岗位</div>
			</template>
			<el-form ref="userDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="用户名" prop="username">
							<el-input v-model="state.ruleForm.username" disabled clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="昵称" prop="nickName">
							<el-input v-model="state.ruleForm.nickName" disabled clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属部门" prop="deptId">
							<el-select v-model="state.ruleForm.deptId" placeholder="请选择用户所属部门" style="width: 100%">
								<el-option v-for="item in state.ruleForm.allDeptList" :key="item.id" :label="item.deptName" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="所属岗位" prop="postId">
							<el-select v-model="state.ruleForm.postId" placeholder="请选择用户所属岗位" style="width: 100%">
								<el-option v-for="item in state.ruleForm.allPostList" :key="item.id" :label="item.name" :value="item.id" />
							</el-select>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<div class="flex row-center mt10 mb10">
				<div class="btn3" @click="onCancel">
					<el-icon> <ele-Close /> </el-icon><span class="ml6">取 消</span>
				</div>
				<div class="btn1 ml10" @click="onSubmit()">
					<el-icon> <ele-Check /> </el-icon><span class="ml6">保 存</span>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="assignDeptPost">
import { reactive, ref, nextTick } from 'vue';
import { ElNotification } from 'element-plus';
import { useDeptApi } from '/@/api/system/dept/index';
import { usePostApi } from '/@/api/system/post/index';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const userDialogFormRef = ref();
const state: any = reactive({
	ruleForm: {
		userId: '', // 用户id
		deptId: '', // 部门id
		postId: '', // 岗位id
		username: '', // 用户名
		nickName: '', // 昵称
	},
	allDeptList: [], // 部门列表
	allPostList: [], // 岗位列表
	dialog: {
		isShowDialog: false,
	},
	rules: {
		deptId: [{ required: true, message: '请选择用户所属部门', trigger: 'blur' }],
		postId: [{ required: true, message: '请选择用户所属岗位', trigger: 'blur' }],
	},
});

// 打开弹窗
const openDialog = (row: any) => {
	state.ruleForm = {
		userId: row.id, // 用户id
		deptId: row.deptId ? row.deptId : '', // 部门id
		postId: row.postId ? row.postId : '', // 岗位id
		username: row.username, // 用户名
		nickName: row.nickName, // 昵称
	};
	getDeptList();
	getPostList();
	state.dialog.isShowDialog = true;
};

// 获取部门列表
const getDeptList = async () => {
	const { data } = await useDeptApi().getListDept({});
	state.ruleForm.allDeptList = data;
};

// 获取岗位列表
const getPostList = async () => {
	const { data } = await usePostApi().getPostList();
	state.ruleForm.allPostList = data.sort((a: any, b: any) => b.sort - a.sort);
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
	closeDialog();
};

// 表单清空
const resetData = () => {
	nextTick(() => {
		userDialogFormRef.value.resetFields();
		state.ruleForm = {
			userId: '', // 用户id
			deptId: '', // 部门id
			postId: '', // 岗位id
			username: '', // 用户名
			nickName: '', // 昵称
		};
	});
};

// 分配
const doAssign = async () => {
	const { userId, deptId, postId } = state.ruleForm;
	const data = await useDeptApi().addDeptUserAndPost({
		userId,
		deptId,
		postId,
	});
	if (data.code == 200) {
		ElNotification({
			title: '提示',
			message: '分配用户岗位和部门成功',
			position: 'top-right',
			type: 'success',
		});
		emit('refresh');
	}
};

// 提交
const onSubmit = () => {
	userDialogFormRef.value.validate((valid: any) => {
		if (valid) {
			doAssign();
		} else {
			ElNotification({
				title: '提示',
				message: '校验失败,请填写完整用户信息',
				position: 'top-right',
				type: 'error',
			});
		}
		closeDialog();
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
<style scoped lang="scss"></style>
